package com.yawn;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;

/**
 * @author Created by yawn on 2017-12-10 9:30
 */
public class TestProcessDefine {

    public static void main(String[] args) {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        // 拿到存储服务和运行时服务
        RepositoryService repositoryService = processEngine.getRepositoryService();
        RuntimeService runtimeService = processEngine.getRuntimeService();

        // 部署流程定义文件
        Deployment deployment = repositoryService.createDeployment().addClasspathResource("demo4.bpmn").deploy();

        // 查询流程定义
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .deploymentId(deployment.getId()).singleResult();
        System.out.println(processDefinition);
        // 启动一个流程实例
        ProcessInstance instance = runtimeService.startProcessInstanceById(processDefinition.getId());
        System.out.println(instance);


        // 中止流程定义
        if (!processDefinition.isSuspended()) {
            repositoryService.suspendProcessDefinitionById(processDefinition.getId());
            /// by key
//            repositoryService.suspendProcessDefinitionByKey(processDefinition.getKey());
        }
        // 中止流程定义后无法起动流程实例
        try {
            ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinition.getId());
            System.out.println(processInstance);
        } catch (Exception e) {
            e.printStackTrace();
        }

///
//        // 重新查询流程定义
//        processDefinition = repositoryService.createProcessDefinitionQuery()
//                .deploymentId(deployment.getId()).singleResult();
//        // 激活流程定义
//        if (processDefinition.isSuspended()) {
//            runtimeService.activateProcessInstanceById(processDefinition.getId());
//        }
//        // 激活流程定义后可以起动流程实例
//        ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinition.getId());
//        System.out.println(processInstance);


        processEngine.close();
    }
}
